import os

# output = os.popen("ls").read()
# print(output.strip())

total=31
a=7
b=24

exe = '1d-heat'

os.system(f'rm  -f result/{exe}.log')

f = open(f'result/{exe}.log', 'w')

bt_list = list(range(1, 17))
bt_list += list(range(20, 33, 2))
bt_list += list(range(36, 129, 4))

for n in range(a, b + 1):
    last_bx_perf = -1
    bx_dec_cnt = 0
    for log_bx in range(n, 1, -1):
        bx_perfs = []
        last_perf = -1
        dec_cnt = 0
        for bt in bt_list:
            N, T, BX, BT = 2**n, 2**(total - n), 2**log_bx, bt
            print(f'N = {N}, T = {T}, BX = {BX}, BT = {BT}, ', end='')
            output = os.popen(f'../{exe}/exe-{exe} {N} {T} {BX} {BT}').read()
            f.write(f'N = {N}, T = {T}, BX = {BX}, BT = {BT}, ')
            if '\n' not in output: # wrong
                f.write('\n')
            else:
                f.write(output)
                print(output.strip())
                perf = output.strip().split(' = ')[-1]
                if 'inf' in perf or float(perf) > 1000: 
                    break

                perf = float(perf)
                bx_perfs.append(perf)
                if perf < last_perf:
                    dec_cnt += 1
                else:
                    dec_cnt = max(0, dec_cnt - 1)
                print(last_perf, perf, dec_cnt)
                last_perf = perf
                # if dec_cnt >= 3:
                #     break
        if len(bx_perfs) == 0:
            continue
        cur_bx_perf = max(bx_perfs)
        if cur_bx_perf < last_bx_perf:
            bx_dec_cnt += 1
        else:
            bx_dec_cnt = max(0, bx_dec_cnt - 1)
        print('bx: ', last_bx_perf, cur_bx_perf, bx_dec_cnt)
        last_bx_perf = cur_bx_perf
        # if bx_dec_cnt >= 3:
        #     break
        
        f.flush()
f.close()
